package com.yoso.shop.application.service.impl;

import com.alibaba.fastjson.JSON;
import com.yoso.shop.application.service.IConsumerService;
import com.yoso.shop.domain.user.consumer.ConsumerEntity;
import com.yoso.shop.domain.user.consumer.ConsumerEntityExample;
import com.yoso.shop.infrastructure.base.service.BaseService;
import com.yoso.shop.infrastructure.dao.user.consumer.ConsumerEntityMapper;
import com.yoso.shop.interfaces.vo.user.consumer.ConsumerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @author shizejin
 * @date 2021/11/17
 * @description:
 */
@Service
public class ConsumerServiceImpl extends BaseService<ConsumerEntity, ConsumerVo, ConsumerEntityExample> implements IConsumerService {
    @Autowired
    private ConsumerEntityMapper consumerEntityMapper;
    @Override
    public ConsumerEntity V2E(ConsumerVo data) {
        return JSON.parseObject(JSON.toJSONString(data),ConsumerEntity.class);
    }

    @Override
    public ConsumerVo E2V(ConsumerEntity data) {
        return JSON.parseObject(JSON.toJSONString(data),ConsumerVo.class);

    }

    @Override
    public ConsumerEntityExample getEntityExample(Map<String,String> paramMap) {
        return new ConsumerEntityExample();
    }

    /**
     * 用户注册
     *
     * @param
     * @return
     */
    @Override
    public boolean register(ConsumerEntity entity) {
        ConsumerEntityExample example = new ConsumerEntityExample();
        example.createCriteria()
                .andUsernameEqualTo(entity.getUsername());
        List<ConsumerEntity> result = consumerEntityMapper.selectByExample(example);

        if (CollectionUtils.isEmpty(result)) {
            consumerEntityMapper.insertSelective(entity);
        }
        return CollectionUtils.isEmpty(result);
    }

    /**
     * 用户登录
     *
     * @param entity
     * @return
     */
    @Override
    public ConsumerVo login(ConsumerEntity entity) {
        ConsumerEntityExample example = new ConsumerEntityExample();
        example.createCriteria()
                .andUsernameEqualTo(entity.getUsername())
                .andPasswordEqualTo(entity.getPassword());
        List<ConsumerEntity> result = consumerEntityMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(result)){
            return null;
        }
        return E2V(result.get(0));
    }
}
